var Promise = require('bluebird');

var mongoBase = require('../common/mongo-base');
var page = require('../common/page');
var utils = require('../common/utils');

module.exports = {
  newsEditSearch: newsEditSearch,
  newsChooseSearch: newsChooseSearch,
  planeSearch: planeSearch,
  planeEditSearch: planeEditSearch,
  planeChooseSearch: planeChooseSearch
};


function newsEditSearch(req, res, next) {

  var keyword = req.query.keyword;
  var pageNo = Math.floor(req.query.pageNo);
  var pageSize = Math.floor(req.query.pageSize);
  pageNo = pageNo ? pageNo: 1;
  pageSize = pageSize ? pageSize: 20;

  if (!keyword) {
    res.redirect('/news/list');
    return;
  };

  var query = {
    "$or": [{"artibodyTitle": new RegExp(keyword)}, {"keywords": new RegExp(keyword)}, {"artibody": new RegExp(keyword)}]
  };
  var fields = {_id:1,  artibodyTitle:1, mediaName:1, pubDate:1, medialink:1};
  var options = {sort:{date:-1}};

  mongoBase.findData('news-article', query, fields, options)

    .then(function(data) {

      var newsList = utils.getSearchlist(data, pageNo, pageSize);

      var resData = {
        type: 'news',
        title: '航空资讯列表',
        searchUrl: '/admin/newsEditSearch',
        showUrl: '/admin/showNewsArticle',
        removeUrl: '/admin/removeNewsArticle',
        pageUrl: '/admin/newsEditSearch',
        keyword: keyword,
        list: newsList,
        totalCount: data.length
      }
      /*分页数据*/
      resData.page = page.getPage(pageNo, pageSize, resData.totalCount, 7);

      res.render('admin/article-edit-list', resData);

    })

    .catch(function(err){
      console.log(err);
    });
}




function newsChooseSearch(req, res, next) {

  var keyword = req.query.keyword;
  var pageNo = Math.floor(req.query.pageNo);
  var pageSize = Math.floor(req.query.pageSize);
  pageNo = pageNo ? pageNo: 1;
  pageSize = pageSize ? pageSize: 20;

  var chooseLink = req.query.chooseLink;
  var parentId = req.query.parentId;

  if (!keyword) {
    res.redirect('/news/list');
    return;
  };

  var query = {
    hisPhoto: true,
    "$or": [{"artibodyTitle": new RegExp(keyword)}, {"keywords": new RegExp(keyword)}, {"artibody": new RegExp(keyword)}]
  };
  var fields = {_id:1,  artibodyTitle:1, mediaName:1, pubDate:1, medialink:1};
  var options = {sort:{date:-1}};

  mongoBase.findData('news-article', query, fields, options)

    .then(function(data) {

      var newsList = utils.getSearchlist(data, pageNo, pageSize);

      var resData = {
        type: 'news',
        title: '航空资讯列表',
        searchUrl: '/admin/newsChooseSearch',
        pageUrl: '/admin/newsListHasPhoto',
        keyword: keyword,
        list: newsList,
        totalCount: data.length,
        chooseLink: chooseLink
      }
      if(parentId) {
        resData.parentId = parentId;
      }
      /*分页数据*/
      resData.page = page.getPage(pageNo, pageSize, resData.totalCount, 7);

      res.render('admin/article-choose-list', resData);

    })

    .catch(function(err){
      console.log(err);
    });
}




function planeSearch(req, res, next) {

  var keyword = req.query.keyword;
  var pageNo = Math.floor(req.query.pageNo);
  var pageSize = Math.floor(req.query.pageSize);
  pageNo = pageNo ? pageNo: 1;
  pageSize = pageSize ? pageSize: 20;

  if (!keyword) {
    res.redirect('/plane/list');
    return;
  };

  var query = {
    "$or": [{"planeName": new RegExp(keyword)}, {"ellipsis": new RegExp(keyword)}, {"parameters.value": new RegExp(keyword)}, {"others.name": new RegExp(keyword)}, {"others.description": new RegExp(keyword)}]
  };

  var fields = {_id:1, manufacturer:1, firstTime:1, planeName:1, parameters:1};
  var options = {sort:{date:-1}};


  mongoBase.findData('plane-data', query, fields, options)

    .then(function(data) {
      var planeList = utils.getSearchlist(data, pageNo, pageSize);
      var resData = {
        searchUrl: '/admin/planeSearch',
        pageUrl: '/admin/planeSearch',
        keyword: keyword,
        planeList: planeList,
        totalCount: data.length
      }
      /*分页数据*/
      resData.page = page.getPage(pageNo, pageSize, resData.totalCount, 7);

      res.render('admin/plane-list', resData);
    })

    .catch(function(err){
      console.log(err);
    });
}


function planeEditSearch(req, res, next) {

  
  var keyword = req.query.keyword;
  var pageNo = Math.floor(req.query.pageNo);
  var pageSize = Math.floor(req.query.pageSize);
  pageNo = pageNo ? pageNo: 1;
  pageSize = pageSize ? pageSize: 20;

  if (!keyword) {
    res.redirect('/plane/list');
    return;
  };

  var query = {
    "$or": [{"artibodyTitle": new RegExp(keyword)}, {"keywords": new RegExp(keyword)}, {"artibody": new RegExp(keyword)}]
  };
  var fields = {_id:1,  artibodyTitle:1, mediaName:1, pubDate:1, medialink:1};
  var options = {sort:{date:-1}};

  mongoBase.findData('plane-article', query, fields, options)

    .then(function(data) {

      var planeList = utils.getSearchlist(data, pageNo, pageSize);

      var resData = {
        type: 'plane',
        title: '飞机详细介绍列表',
        searchUrl: '/admin/planeEditSearch',
        showUrl: '/admin/showPlaneArticle',
        removeUrl: '/admin/removePlaneArticle',
        pageUrl: '/admin/planeEditSearch',
        keyword: keyword,
        list: planeList,
        totalCount: data.length
      }
      /*分页数据*/
      resData.page = page.getPage(pageNo, pageSize, resData.totalCount, 7);

      res.render('admin/article-edit-list', resData);

    })

    .catch(function(err){
      console.log(err);
    });
}




function planeChooseSearch(req, res, next) {

   var keyword = req.query.keyword;
  var pageNo = Math.floor(req.query.pageNo);
  var pageSize = Math.floor(req.query.pageSize);
  pageNo = pageNo ? pageNo: 1;
  pageSize = pageSize ? pageSize: 20;

  var chooseLink = req.query.chooseLink;
  var parentId = req.query.parentId;

  if (!keyword) {
    res.redirect('/plane/list');
    return;
  };

  var query = {
    "$or": [{"artibodyTitle": new RegExp(keyword)}, {"keywords": new RegExp(keyword)}, {"artibody": new RegExp(keyword)}]
  };
  var fields = {_id:1,  artibodyTitle:1, mediaName:1, pubDate:1, medialink:1};
  var options = {sort:{date:-1}};

  mongoBase.findData('plane-article', query, fields, options)

    .then(function(data) {

      var planeList = utils.getSearchlist(data, pageNo, pageSize);

      var resData = {
        type: 'plane',
        title: '飞机详细介绍列表',
        searchUrl: '/admin/planeChooseSearch',
        pageUrl: '/admin/planeListChoose',
        keyword: keyword,
        list: planeList,
        totalCount: data.length,
        chooseLink: chooseLink
      }
      if(parentId) {
        resData.parentId = parentId;
      }
      /*分页数据*/
      resData.page = page.getPage(pageNo, pageSize, resData.totalCount, 7);

      res.render('admin/article-choose-list', resData);

    })

    .catch(function(err){
      console.log(err);
    });
}
